#include "MarsPrecompile.h"

#include "MarsVertexData.h"
#include "MarsVRamBuffer.h"
#include "MarsInputLayout.h"

using namespace Graphics;

VertexData::~VertexData()
{
	SAFE_DELETE( m_pInputLayout );

	DeleteVRamBuffer();

	SAFE_DELETE_ARRAY( m_pMemVertexBuffer );
	m_iVertexBufferSize = 0;

	SAFE_DELETE_ARRAY( m_pMemIndexBuffer );
	m_iIndexBufferSize = 0;
}

bool VertexData::CreateVRamBuffer()
{
	if ( m_pMemVertexBuffer && m_iVertexBufferSize != 0 )
	{
		m_pVRamVertexBuffer = new VRamBuffer( VRamBuffer::BufferType_Vertex, D3D11_USAGE_DEFAULT );
		m_pVRamVertexBuffer->Create( m_pMemVertexBuffer, m_iVertexBufferSize );
	}

	if ( m_pMemIndexBuffer && m_iIndexBufferSize != 0 )
	{
		m_pVRamIndexBuffer = new VRamBuffer( VRamBuffer::BufferType_Index, D3D11_USAGE_DEFAULT );
		m_pVRamIndexBuffer->Create( m_pMemIndexBuffer, m_iIndexBufferSize );
	}

	return true;
}

void VertexData::DeleteVRamBuffer()
{
	SAFE_DELETE( m_pVRamVertexBuffer );
	SAFE_DELETE( m_pMemIndexBuffer );
}

DXGI_FORMAT VertexData::GetIndexBufferFormat() const
{
	DXGI_FORMAT eNativeFormat = DXGI_FORMAT_R16_UINT;

	if ( m_eIndexBufferFormat == IBF_UInt8 )
	{
		eNativeFormat = DXGI_FORMAT_R8_UINT;
	}
	else if ( m_eIndexBufferFormat == IBF_UInt16 )
	{
		eNativeFormat = DXGI_FORMAT_R16_UINT;
	}
	else if ( m_eIndexBufferFormat == IBF_UInt32 )
	{
		eNativeFormat = DXGI_FORMAT_R32_UINT;
	}

	return eNativeFormat;
}
